package com.lancoo.answer.widget.combinationView;

import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.media.MediaPlayer;
import android.os.Looper;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.lancoo.answer.R;
import com.lancoo.answer.manager.MediaManager;
import com.lancoo.answer.model.ConstantBean;
import com.lancoo.answer.model.entity.TaskControlBean;
import com.lancoo.answer.helper.ToastHelper;
import com.lancoo.answer.util.ExamFontSizeAdjustUtils;
import com.lancoo.answer.widget.audioPlayView.AudioPlayCallBack;

/**
 * Created by pengmi on 2021/12/27 16:39
 * Description:小题、作答音频显示、播放组合view
 */
public class ChildAudioView extends RelativeLayout implements View.OnClickListener{

    private static final String TAG = "ChildAudioView";
    private ImageView img_horn;
    private MediaManager mediaManager;
    private long subTime;
    private AudioPlayCallBack callBack;
    private String audioPath;
    private boolean isError;

    public ChildAudioView(Context context) {
        super(context);
        init(context);
    }

    public ChildAudioView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public ChildAudioView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context){
        LayoutInflater.from(context).inflate(R.layout.ev_layout_child_audio_view,this);
        findViewById(R.id.rl_horn).setOnClickListener(this);
        img_horn = findViewById(R.id.img_horn);
        changeUIStyle();
        adjustFontSize();
    }

    public void setAudioPath(String audioPath){
        if(audioPath==null){
            Log.e(TAG,"child is null");
            return;
        }
        this.audioPath=audioPath;
    }

    /**
     * 设置文字
     * @param resID res
     */
    public void setAudioTxt(int resID){
        TextView tv_audio = findViewById(R.id.tv_audio);

        try{
            tv_audio.setText(resID);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public void pause(){
        Log.e(TAG,"pause");
        if(mediaManager!=null){
            mediaManager.pause();
        }
        showPlayState(false);
    }

    public void release(){
        if(mediaManager!=null){
            mediaManager.release();
            mediaManager=null;
        }
        callBack=null;
    }


    @Override
    public void onClick(View v) {
        if(System.currentTimeMillis()-subTime<500){
            return;
        }
        subTime= System.currentTimeMillis();
        TaskControlBean taskControlBean = ConstantBean.Companion.getTaskControlBean();
        if(taskControlBean==null){
            return;
        }
        int enableAnwer = taskControlBean.getEnableAnwer();
        if(enableAnwer==0){
            return;
        }
        if(mediaManager!=null){
            boolean prepared = mediaManager.isPrepared();
            if(!prepared&&!isError){
                Log.e("播放","还未准备好。。。");
                ToastHelper.showAudioLoadingToast(getContext());
                return;
            }
        }
        if(mediaManager==null||!mediaManager.isPlay()){
            if(callBack!=null){
                callBack.onAudioPlay();
            }
            startPlayRecordSound();
            showPlayState(true);
        }else{
            showPlayState(false);
            mediaManager.pause();
        }
    }



    private void showPlayState(boolean isPlaying){
        if( Looper.myLooper() != Looper.getMainLooper()){
            return;
        }
        if(img_horn!=null&&img_horn.getVisibility()== View.VISIBLE){
            if(isPlaying){
                img_horn.setBackground(null);
                ((AnimationDrawable)img_horn.getDrawable()).start();
            }else{
                ((AnimationDrawable)img_horn.getDrawable()).stop();
                ((AnimationDrawable)img_horn.getDrawable()).selectDrawable(0);
            }
        }
    }


    private void startPlayRecordSound(){
        if(getContext()==null){
            return;
        }
        if(mediaManager==null){
            mediaManager=new MediaManager(getContext());
        }
        String url=audioPath;
        if(mediaManager.isPrepared()){
            mediaManager.start();
            return;
        }
        mediaManager.setUpMedia(url, new MediaManager.OnPlayListener() {
            @Override
            public void onComplete() {
                showPlayState(false);
            }

            @Override
            public void onStartError() {
                ToastHelper.showAudioLoadErrorToast(getContext());
                showPlayState(false);
                isError=true;
            }

            @Override
            public void onPausePlay() {

            }

            @Override
            public void onSourceError() {
                ToastHelper.showAudioLoadErrorToast(getContext());
                showPlayState(false);
                isError=true;
            }

            @Override
            public void onStopPlay() {

            }

            @Override
            public void onPrepare() {
                isError=false;
                if(mediaManager!=null){
                    mediaManager.start();
                }
            }

            @Override
            public void onBufferingUpdate(MediaPlayer mp, int percent) {
                Log.e(TAG,"onBufferingUpdate:"+percent);
            }
        });
    }

    public void setCallBack(AudioPlayCallBack callBack) {
        this.callBack = callBack;
    }

    public void changeUIStyle(){
        TaskControlBean taskControlBean = ConstantBean.Companion.getTaskControlBean();
        if(taskControlBean==null){
            return;
        }
        int enableAnwer = taskControlBean.getEnableAnwer();
        View rl_horn=findViewById(R.id.rl_horn);
        if(enableAnwer==0){
            rl_horn.setAlpha(0.3f);
        }else{
            rl_horn.setAlpha(1f);
        }

    }


    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        pause();
        release();
    }

    @Override
    protected void onWindowVisibilityChanged(int visibility) {
        super.onWindowVisibilityChanged(visibility);
    }

    public void adjustFontSize(){
        TextView tv_audio = findViewById(R.id.tv_audio);
        ExamFontSizeAdjustUtils.adjustTextViewFontSize(tv_audio,13);
    }
}
